package com.zhongjiananbang.plugstartactivitydemo;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.zhongjiananbang.standardlibeary.RenInterface;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 专门去加载插件的activity
 */
public class ProxyActivity extends AppCompatActivity {

    private String className;
    private String entryActivityName;
    private RenInterface renInterface;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

        className = getIntent().getStringExtra("className");
//        PluginManager.getInstance().loadPath(className);
        try {
            Class activityClass = getClassLoader().loadClass(className);
            Constructor constructor = activityClass.getConstructor(new Class[]{});
            Object instance = constructor.newInstance(new Object[]{});
            renInterface = (RenInterface) instance;
            renInterface.attach(this);
            Bundle bundle = new Bundle();
            // TODO: 2017/11/15 可以传递信息
            renInterface.onCreate(bundle);

            /**
             * 标准
             * 传递生命周期
             */
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (renInterface != null)
            renInterface.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (renInterface != null)
            renInterface.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (renInterface != null)
            renInterface.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (renInterface != null)
            renInterface.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (renInterface != null)
            renInterface.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (renInterface != null)
            renInterface.onStop();
    }

    /**
     * class 文件  资源文件
     *
     * @return
     */
    @Override
    public ClassLoader getClassLoader() {
        return PluginManager.getInstance().getDexClassLoader();
    }

    @Override
    public Resources getResources() {
        return PluginManager.getInstance().getResources();
    }

    @Override
    public void startActivity(Intent intent) {
        String className = intent.getStringExtra("className");
        Intent intent1=new Intent(this,ProxyActivity.class);
        intent1.putExtra("className",className);
        super.startActivity(intent1);

    }
}
